﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Club
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        string prepage = "Main.aspx";
        protected void Page_Load(object sender, EventArgs e)
        {
            Dictionary<string, object> sessionVar = (Dictionary<string, object>)Session["vars"];
            DAL.User user = (DAL.User)sessionVar["userVar"];
            if (user == null)
            {
                Response.Redirect("Default.aspx?prepage=ChangePassword.aspx");
            }

            if (Request.QueryString["prepage"] != null)
            {
                prepage = Request.QueryString["prepage"];
            }
            this.txtPassword.Focus();
        }

        protected void btnChangePassword_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, object> sessionVar = (Dictionary<string, object>)Session["vars"];                       
                DAL.User user = new DAL.User();
                user.Name = (sessionVar["userVar"] as DAL.User).Name;
                user.Password = txtPassword.Text;
                user.confirmPassword = txtConfirmPassword.Text;
                BLL.ValidateUser validateUser = new BLL.ValidateUser();
                user = validateUser.ChangePassword(user);
                if (user != null)
                {                                      
                    DateTime? l_lastLogout= user.UpdateLogin(user.Id, Request.ServerVariables["REMOTE_ADDR"] as string);
                    Session["LastLogout"] = l_lastLogout;
                }
                WebCommon.ShowMsg(Response,"Password changed successfully!");
                Response.Redirect(prepage);
            }
            catch (Exception ex)
            {
                WebCommon.ShowMsg(this.Response, ex.Message);
            }
        }
    }
}